package com.anbinh.doctor.fragment.account;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.anbinh.doctor.DetailtActivity;
import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.ListAddPermissionAdapter;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.ItemSpecial;
import com.anbinh.doctor.model.ResponsePermissionDetailt;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;

public class FragmentAddPermission extends SherlockFragment {
	private static FragmentAddPermission fragment = null;
	private View mView;
	private ListAddPermissionAdapter adapter;
	private List<com.anbinh.doctor.model.ResponsePermissionDetailt.Item> mListSp;

	private ListView mGridView;
	private CircleProgressBar loadding;
	private ItemSpecial currentDoctorInfo;

	public static FragmentAddPermission newInstance() {
		if (fragment == null) {
			fragment = new FragmentAddPermission();
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}

	@Override
	public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu,
			com.actionbarsherlock.view.MenuInflater inflater) {
		menu.add(0, R.id.dia_diem_kham, Menu.NONE, "Hoàn thành")
				.setIcon(R.drawable.ic_done)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		pushPermission();
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onResume() {
		super.onResume();
		((DetailtActivity) getActivity()).setActionBarTitle(getResources()
				.getString(R.string.thiet_lap_quyen));

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_thongtincanhan, null);

		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		loadding = (CircleProgressBar) mView.findViewById(R.id.loadding);

		mGridView = (ListView) mView.findViewById(R.id.listView1);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				for (int i = 0; i <= mListSp.size(); i++) {
					if (position == i) {
						mListSp.get(position).setChecked(
								!mListSp.get(position).isChecked());
					}
				}
				adapter.notifyDataSetChanged();

			}
		});

		onLoadData();
	}

	public void pushPermission() {
		loadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);
		UserLogin userLogin = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		String token = userLogin.getData().getToken();
		ServerController.getInstance(getActivity()).requestAddPermission(
				token,
				FragmentAssistantDetailt.newInstance().getCurrentPermission()
						.getId(),
				FragmentAssistantList.newInstance().getCurrentAssistants()
						.getId(), getListPermission(),
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						loadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);
						Utils.showMessage(getActivity(), data.getDescription());
						/*
						 * Intent intent = new Intent();
						 * intent.putExtra(Utils.KEY, Utils.RESUME_EXTRA_INFO);
						 * getActivity().setResult(Integer.MAX_VALUE, intent);
						 * getActivity().finish();
						 */
					}

					@Override
					public void onError(int errorCode, String message) {
						loadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);
					}

				});
	}

	private String getListPermission() {
		String result = "";
		for (int i = 0; i < mListSp.size(); i++) {
			if (mListSp.get(i).isChecked()) {
				result = mListSp.get(i).getName() + "," + result;
			}
		}
		if (result.isEmpty())
			return result;
		return result.substring(0, result.length() - 1);
	}

	private void onLoadData() {
		loadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);
		UserLogin userLogin = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		String token = userLogin.getData().getToken();
		ServerController.getInstance(getActivity())
				.requestPermissionDetailtList(
						token,
						FragmentAssistantDetailt.newInstance()
								.getCurrentPermission().getId(),
						FragmentAssistantList.newInstance()
								.getCurrentAssistants().getId(),
						new OnRequest<ResponsePermissionDetailt>() {

							@Override
							public void onSuccess(ResponsePermissionDetailt data) {
								loadding.setVisibility(View.GONE);
								Utils.disableEnableControls(true, mView);
								if (data.getResponsecode() >= 0) {
									mListSp = data.getData().getPermission()
											.getPermissions();
									adapter = new ListAddPermissionAdapter(
											getActivity(),
											R.layout.seprator_chuyen_khoa,
											mListSp, 0);
									mGridView.setAdapter(adapter);

								}
							}

							@Override
							public void onError(int errorCode, String message) {
								// TODO Auto-generated method stub

							}
						});
	}

	public ItemSpecial getCurrentDoctorInfo() {
		return currentDoctorInfo;
	}

	public void updateInfo() {
		// TODO Auto-generated method stub

	}

}
